.globl lock_acquire, lock_release

/* 
  typedef volatile struct {
    uint32_t lock;
    uint32_t rflags;
  } lock_t;
*/

//  void lock_acquire(lock_t *lock);
lock_acquire:
  pushfq
  cli

  popq %rax
  movl %eax, 4(%rdi)

  _acquire:
  lock btsl $0, (%rdi)
  jc _spin
  ret
  
  _spin:
  pause
  testl $1, (%rdi)
  jnz _spin
  jmp _acquire

//  void lock_release(lock_t *lock);
lock_release:
  movl $0, (%rdi)

  xorq %rax, %rax
  movl 4(%rdi), %eax
  pushq %rax
  popfq

  ret
